<template>
  <div style="width: 100%; height: 100%; position: relative">
    <div :id="idname" style="width: 100%; height: 100%" v-if="!isShow"></div>
    <div class="no_data" v-show="isShow">
      <img ref="img" src="@/assets/images/no_data.png" alt="" />
      <p>暂无数据</p>
    </div>
  </div>
</template>

<script>
import echarts from "echarts";
import { EleResize } from "@/utils/esresize.js";
export default {
  props: {
    idname: {
      type: String,
      required: true,
    },
    datalist: {
      type: Object,
      default: {},
    },
  },
  data() {
    return {
      isShow: false,
    };
  },
  watch: {
    datalist: {
      handler(val, oldVal) {
        let newarr = val.data.filter((item) => item != 0);
        if (newarr.length) {
          this.$nextTick(() => {
            this.chartColumn(1);
          });
        } else {
          this.$nextTick(() => {
            this.chartColumn(2);
          });
        }
      },
      deep: true,
    },
  },
  methods: {
    // 只适用图表方法
    drawLine(dom, option) {
      if (document.getElementById(dom)) {
        let myChart = echarts.init(document.getElementById(dom));
        let resizeDiv = document.getElementById(dom);
        myChart.clear();
        myChart.setOption(option);
        let listener = function () {
          myChart.resize();
        };
        EleResize.on(resizeDiv, listener);
      }
    },
    chartColumn(type) {
      // 折线图
      let dom = this.idname;
      let option1 = {
        graphic: [
          {
            type: "image",
            id: "logo",
            left: "center",
            top: "50",
            bounding: "raw",
            style: {
              image: this.$refs["img"].currentSrc,
              width: 92,
              opacity: 1,
            },
          },
        ],
      };
      let option2 = {
        tooltip: {
          show: true,
          trigger: "axis",
          formatter: "{a} <br/>{b} : {c}",
          axisPointer: {
            type: "cross",
            label: {
              backgroundColor: "#03A98E",
            },
          },
        },
        grid: {
          top: 20,
          bottom: 20,
          left: 60,
          right: 40,
        },
        xAxis: {
          type: "category",
          boundaryGap: false,
          axisLabel: {
            show: true,
            textStyle: {
              color: "#333",
            },
          },
          axisTick: {
            show: false,
          },
          data: this.datalist.title,
        },
        yAxis: {
          axisLine: {
            lineStyle: {
              color: "rgba(107,107,107,0.37)",
            },
          },
          splitLine: {
            show: true,
            lineStyle: {
              color: "#ccc",
              type: "dashed",
            },
          },
          axisLabel: {
            show: true,
            textStyle: {
              color: "#333",
            },
          },
          axisTick: {
            show: false,
          },
          type: "value",
        },
        series: [
          {
            name: this.datalist.series.name,
            data: this.datalist.data,
            symbol: "circle",
            symbolSize: 10,
            smooth: true, //这句就是让曲线变平滑的
            type: "line",
            lineStyle: {
              normal: {
                color: "#03A98E",
              },
            },
            itemStyle: {
              normal: {
                borderWidth: 3,
                borderColor: "#BAFFF4",
                color: "#03A98E",
                areaStyle: {
                  color: new echarts.graphic.LinearGradient(0, 1, 0, 0, [
                    {
                      offset: 0,
                      color: "rgba(3,169,142,0)",
                    },
                    {
                      offset: 1,
                      color: "rgba(3,169,142,0.25)",
                    },
                  ]),
                },
              },
            },
          },
        ],
      };
      let option = type == 1 ? option2 : option1;
      this.drawLine(dom, option);
    },
  },
  mounted() {
    this.chartColumn();
  },
};
</script>

<style></style>
